package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.ArticleQueryDTO;
import com.jeff.tianti.cms.dto.ColumnInfoDTO;
import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.entity.Article;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.service.ArticleService;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * CMS的Controller
 *
 * @author Jeff Xu
 */
@Controller
@RequestMapping("/law")
public class LawController {

    @Autowired
    private ColumnInfoService columnInfoService;

    @Autowired
    private ArticleService articleService;

    /**
     * 获取文章列表
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/article/list")
    public String articleList(HttpServletRequest request, Model model) {
        String type = request.getParameter("type");
        String rootColumnId = request.getParameter("rootColumnId");
        String columnId = request.getParameter("columnId");
        String title = request.getParameter("title");
        String publisher = request.getParameter("publisher");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String createDateSortCss = request.getParameter("createDateSortCss");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        List<ColumnInfo> flfkCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK); // 法律风控
        if (!CollectionUtils.isEmpty(flfkCoulumnInfo)) {
            ColumnInfo flfk = flfkCoulumnInfo.get(0);
            rootColumnId = flfk.getId();
        }


        String  ggfgId ="";
        List<ColumnInfo> ggfgCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK_GGFG); // 法律法规
        if (!CollectionUtils.isEmpty(ggfgCoulumnInfo)) {
            ColumnInfo ggfg = ggfgCoulumnInfo.get(0);
            ggfgId = ggfg.getId();
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(ggfgId);
        columnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> ggfgCoulumnInfoList = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);

       String  bgtId ="";
        List<ColumnInfo> bgtCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK_BGT);//曝光台
        if (!CollectionUtils.isEmpty(bgtCoulumnInfo)) {
            ColumnInfo bgt = bgtCoulumnInfo.get(0);
            bgtId = bgt.getId();
        }
        ColumnInfoQueryDTO bgtIdColumnInfoQueryDTO = new ColumnInfoQueryDTO();
        bgtIdColumnInfoQueryDTO.setRootColumnId(bgtId);
        bgtIdColumnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> bgtCoulumnInfoList = columnInfoService.queryColumnInfoList(bgtIdColumnInfoQueryDTO);
        List<ColumnInfo> columnInfoList=   new ArrayList<>();
       if (!CollectionUtils.isEmpty(bgtCoulumnInfoList)){
           columnInfoList.addAll(ggfgCoulumnInfoList);
           columnInfoList.addAll(bgtCoulumnInfoList);
       }


        ArticleQueryDTO articleQueryDTO = new ArticleQueryDTO();
        articleQueryDTO.setColumnId(columnId);
        articleQueryDTO.setType(type);
        articleQueryDTO.setIsFront(false);
        articleQueryDTO.setRootColumnId(rootColumnId);
        articleQueryDTO.setTitle(title);
        articleQueryDTO.setPublisher(publisher);
        articleQueryDTO.setStartDate(startDate);
        articleQueryDTO.setEndDate(endDate);
        articleQueryDTO.setCreateDateSortCss(createDateSortCss);
        articleQueryDTO.setCurrentPage(currentPage);
        articleQueryDTO.setPageSize(pageSize);
        articleQueryDTO.setDeleteFlag("0");


        PageModel<Article> page = this.articleService.queryArticlePage(articleQueryDTO);
        List<Map<String, Object>> statisMapList = this.articleService.queryStatisMapList(articleQueryDTO);
        Map<String, Object> statisMap = null;
        if (statisMapList != null && statisMapList.size() > 0) {
            statisMap = statisMapList.get(0);
        }
        model.addAttribute("page", page);
        model.addAttribute("type", type);
        model.addAttribute("statisMap", statisMap);
        model.addAttribute("articleQueryDTO", articleQueryDTO);
        model.addAttribute("columnInfoList", columnInfoList);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_LAW_LIST);

        return "/cms/law/law_list";
    }

    /**
     * 文章修改
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/article/edit")
    public String articleEdit(HttpServletRequest request, Model model) {
        Article article = null;
        String id = request.getParameter("id");
        String rootColumnId="";
        String columnId = request.getParameter("columnId");
        if (StringUtils.isNotBlank(id)) {
            article = this.articleService.find(id);
            if (article != null) {
                columnId = article.getColumnInfo().getId();
            }
        }

        List<ColumnInfo> flfkCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK);
        if (!CollectionUtils.isEmpty(flfkCoulumnInfo)) {
            ColumnInfo flfk = flfkCoulumnInfo.get(0);
            rootColumnId = flfk.getId();
        }

        String  ggfgId ="";
        List<ColumnInfo> ggfgCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK_GGFG);
        if (!CollectionUtils.isEmpty(ggfgCoulumnInfo)) {
            ColumnInfo ggfg = ggfgCoulumnInfo.get(0);
            ggfgId = ggfg.getId();
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(ggfgId);
        columnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> ggfgCoulumnInfoList = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);

        String  bgtId ="";
        List<ColumnInfo> bgtCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK_BGT);//曝光台
        if (!CollectionUtils.isEmpty(bgtCoulumnInfo)) {
            ColumnInfo bgt = bgtCoulumnInfo.get(0);
            bgtId = bgt.getId();
        }
        ColumnInfoQueryDTO bgtIdColumnInfoQueryDTO = new ColumnInfoQueryDTO();
        bgtIdColumnInfoQueryDTO.setRootColumnId(bgtId);
        bgtIdColumnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> bgtCoulumnInfoList = columnInfoService.queryColumnInfoList(bgtIdColumnInfoQueryDTO);
        List<ColumnInfo> columnInfoList=   new ArrayList<>();
        if (!CollectionUtils.isEmpty(bgtCoulumnInfoList)){
            columnInfoList.addAll(ggfgCoulumnInfoList);
            columnInfoList.addAll(bgtCoulumnInfoList);
        }

        model.addAttribute("rootColumnId", rootColumnId);
        model.addAttribute("columnId", columnId);
        model.addAttribute("article", article);
        model.addAttribute("rootCoulumnInfoList", columnInfoList);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_ARTICLE_LIST);

        return "/cms/law/dialog/law_edit";
    }

    /**
     * 文章保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/article/ajax/save")
    @ResponseBody
    public AjaxResult doArticlejaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String rootColumnId = request.getParameter("rootColumnId");
            String columnInfoId = request.getParameter("leafColumnId");
            String title = request.getParameter("title");
            String publisher = request.getParameter("publisher");
            String content = request.getParameter("content");
            String summary = request.getParameter("summary");
            String coverImageUrl = request.getParameter("coverImageUrl");
            String href = request.getParameter("href");
            String orderNoStr = request.getParameter("orderNo");
            String articleTypeStr = request.getParameter("articleType");
            String guidRead = request.getParameter("guidRead");

            Article article = null;
            Integer orderNo = null;
            Integer articleType = null;
            ColumnInfo rootColumnInfo = null;
            ColumnInfo columnInfo = null;
            if (StringUtils.isNotBlank(rootColumnId)) {
                rootColumnInfo = this.columnInfoService.find(rootColumnId);
            }
            if (StringUtils.isNotBlank(columnInfoId)) {
                columnInfo = this.columnInfoService.find(columnInfoId);
            }
            if (StringUtils.isNotBlank(orderNoStr)) {
                orderNo = Integer.parseInt(orderNoStr);
            }
            if (StringUtils.isNotBlank(articleTypeStr)) {
                if (articleTypeStr.equals("contentType")) {
                    articleType = 0;
                } else if (articleTypeStr.equals("hrefType")) {
                    articleType = 1;
                } else if (articleTypeStr.equals("adType")) {
                    articleType = 2;
                }
            }
            if (StringUtils.isNotBlank(id)) {
                article = this.articleService.find(id);
                article.setTitle(title);
                article.setPublisher(publisher);
                article.setContent(content);
                article.setType(articleType);
                article.setRootColumnInfo(rootColumnInfo);
                article.setColumnInfo(columnInfo);
                article.setSummary(summary);
                article.setUpdateDate(new Date());
                article.setOrderNo(orderNo);
                article.setHref(href);
                article.setGuidRead(guidRead);
                if (StringUtils.isNotBlank(coverImageUrl)) {
                    article.setCoverImageUrl(coverImageUrl);
                }
            } else {
                article = new Article();
                article.setTitle(title);
                article.setPublisher(publisher);
                article.setContent(content);
                article.setType(articleType);
                article.setRootColumnInfo(rootColumnInfo);
                article.setColumnInfo(columnInfo);
                article.setSummary(summary);
                article.setCreateDate(new Date());
                article.setViewCount(0);
                article.setDeleteFlag(ColumnInfo.DELETE_FLAG_NORMAL);
                article.setIsAudit(true);
                article.setOrderNo(orderNo);
                article.setHref(href);
                article.setGuidRead(guidRead);
                if (StringUtils.isNotBlank(coverImageUrl)) {
                    article.setCoverImageUrl(coverImageUrl);
                }
            }

            if (StringUtils.isNotBlank(id)) {
                this.articleService.update(article);
            } else {
                this.articleService.save(article);
            }
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * 根据根节点获取叶子节点
     *
     * @param request
     * @return
     */
    @RequestMapping("/column/ajax/getLeafColumn")
    @ResponseBody
    public AjaxResult getLeafColumn(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        String rootColumnId = request.getParameter("rootColumnInfoId");
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setIsRootColumnLike(false);
        columnInfoQueryDTO.setRootColumnId(rootColumnId);
        List<ColumnInfo> columnInfoList = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        ajaxResult.setData(columnInfoList);
        return ajaxResult;
    }

    /**
     * 文章删除
     *
     * @param request
     * @return
     */
    @RequestMapping("/article/ajax/delete")
    @ResponseBody
    public AjaxResult ajaxArticleDelete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            String[] ids = request.getParameterValues("ids");
            String deleteFlag = request.getParameter("deleteFlag");

            if (ids != null && ids.length > 0) {
                for (String id : ids) {
                    Article article = this.articleService.find(id);
                    article.setDeleteFlag(deleteFlag);
                    this.articleService.update(article);
                }
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 文章审核
     *
     * @param request
     * @return
     */
    @RequestMapping("/article/ajax/audit")
    @ResponseBody
    public AjaxResult ajaxArticleAudit(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            Boolean isAudit = false;
            String[] ids = request.getParameterValues("ids");
            String auditFlag = request.getParameter("auditFlag");
            if (StringUtils.isNotBlank(auditFlag) && auditFlag.equals("1")) {
                isAudit = true;
            }
            if (ids != null && ids.length > 0) {
                for (String id : ids) {
                    Article article = this.articleService.find(id);
                    article.setIsAudit(isAudit);
                    this.articleService.update(article);
                }
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 文章置顶
     *
     * @param request
     * @return
     */
    @RequestMapping("/article/ajax/top")
    @ResponseBody
    public AjaxResult ajaxArticleTop(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            Boolean isTop = false;
            String[] ids = request.getParameterValues("ids");
            String topFlag = request.getParameter("topFlag");
            if (StringUtils.isNotBlank(topFlag) && topFlag.equals("1")) {
                isTop = true;
            }
            if (ids != null && ids.length > 0) {
                for (String id : ids) {
                    Article article = this.articleService.find(id);
                    article.setIsTop(isTop);
                    this.articleService.update(article);
                }
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * 跳转到flash控件开启页面
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/article/flashView")
    public String articleFlashView(HttpServletRequest request, Model model) {

        return "/cms/article_flash_view";
    }

}
